/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: fdbus_service.h
 *
 * Purpose: bind fdbus communication as service side
 *
 * Developer:
 *   wen.gu , 2021-05-10
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __FDBUS_SERVICE_H__
#define __FDBUS_SERVICE_H__

#include "service_provider.h"
#include "common_base/fdbus.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
struct ReceiveHandler
{
    ServiceProvider::MethodHandler method = nullptr; /** for method, for field: as setter */
    ServiceProvider::MethodHandler getter = nullptr; /** only for field: as getter*/
};


/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

class FDBusService: public ServiceProvider, public CBaseServer
{
public:
    using ReceiveHandlerMap = std::map<MessageId, ReceiveHandler>;
public:
    FDBusService(const std::string& service_name);
    virtual ~FDBusService();

public:
    bool OfferService() override;
    bool StopService() override;

public:
    bool RegisterReceiveHandler(MessageType type,  MessageId msg_id, MethodHandler handler) override;
    DeserializerPtr GetDeserializer(Message::PayloadPtr payload) override;
    SerializerPtr GetSerializer() override;
    bool Send(MessagePtr msg) override;
    MessagePtr MakeMessage(MessageType type) override;
protected:
    void onInvoke(CBaseJob::Ptr &msg_ref) override;
    /* called when client call subscribe() to register message */
    void onSubscribe(CBaseJob::Ptr &msg_ref) override;

protected:
    void onReceive(ReceiveHandlerMap& receive_map, CBaseJob::Ptr &msg_ref);
    ReceiveHandlerMap* getReceiveMap(MessageId msg_id);
protected:
    bool SendEvent(MessagePtr msg);
    bool SendResponse(MessagePtr msg);
    inline uint32_t getNextSessionId() { return next_session_id_++;}
private:
    FdbSocketId_t socket_id_ = FDB_INVALID_ID;
    std::atomic<uint32_t> next_session_id_ = 0;
    ReceiveHandlerMap method_map_;
    ReceiveHandlerMap field_map_;
};

#endif /** !__FDBUS_SERVICE_H__ */

